package com.mmall.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.dao.GoodsMapper;
import com.mmall.dao.OrderMapper;
import com.mmall.pojo.Goods;
import com.mmall.pojo.Order;

import com.mmall.service.OrderService;

import com.mmall.util.DateUtils;
import com.mmall.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("OrderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    public ServerResponse create(Order order){
        order.setCreateTime(new Date());
        order.setPayStatus(Const.PAY_STATUS_NO);
        order.setStatus(Const.ORDER_STATUS_OPEN);
        if(orderMapper.insertSelective(order)>0){
            return ServerResponse.cerateBySuccess("订单生成成功",order);
        }
        return ServerResponse.createByErrorMessage("订单生成失败");
    }

    public ServerResponse findByUserIdGoodsId(Integer userId,Integer goodsId){
        Order order = orderMapper.selectByUserIdGoodsId(userId,goodsId);
        if(order!=null){
            order.setCreateTimeText(DateUtils.formatDateTime(order.getCreateTime()));
            return ServerResponse.cerateBySuccess("订单详情获取成功",order);
        }
        return ServerResponse.createByErrorMessage("订单详情获取失败");
    }

    public ServerResponse updateOrder(Order order){
        if(orderMapper.updateByPrimaryKeySelective(order)>0){
            return ServerResponse.cerateBySuccessMessage("修改成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    public ServerResponse deleteOrder(Integer orderId){
        if(orderMapper.deleteByPrimaryKey(orderId)>0){
            return ServerResponse.cerateBySuccessMessage("订单删除成功");
        }
        return ServerResponse.createByErrorMessage("订单删除失败");
    }

   public ServerResponse payMonet(Order order){
        int count = orderMapper.updateByPrimaryKeySelective(order);
       Goods goods = new Goods();
       goods.setId(order.getGoodsId());
       goods.setSellStatus(Const.SELL_STATUS_NO);//更改商品状态(下架)
       goods.setUpdateTime(new Date());
       count += goodsMapper.updateByPrimaryKeySelective(goods);
       if(count>0){
           return ServerResponse.cerateBySuccessMessage("付款成功");
       }
       return ServerResponse.createByErrorMessage("付款失败");
    }

   public ServerResponse find(Integer orderId){
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order!=null){
            order.setCreateTimeText(DateUtils.formatDateTime(order.getCreateTime()));
            Goods goods = goodsMapper.selectByPrimaryKey(order.getGoodsId());
            order.setGoodsPictures(goods.getPicture());
            JSONObject jsonObject =JSONObject.parseObject(JsonUtils.objectToJson(order));
            jsonObject.put("des",goods.getDes());
            jsonObject.put("nickName",goods.getNick_name());
//            jsonObject.put("pictures", JSONArray.parseObject(goods.getPicture()));
            return ServerResponse.cerateBySuccess("查詢成功",jsonObject);
        }

        return ServerResponse.createByErrorMessage("查詢失敗");
   }

   public ServerResponse findMyOrderList(Integer userId){
        List<Order> orderList = orderMapper.selectMyList(userId);
        return ServerResponse.cerateBySuccess("查询成功",orderList);
   }
}
